<?php

/**
 * news actions.
 *
 * @package    vtns
 * @subpackage news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cmsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	  // DEFAULT
	  $this->navbars = vtnsSlot::getNavBars();
	  $this->rightBlocks = vtnsSlot::getRightBlocks();
	  $this->menuBottoms = vtnsSlot::getMenuBottoms();
	  $this->bottom = vtnsSlot::getBottom();
	  
	  $cms_category_news = Doctrine_Core::getTable('VTNSCmsCategory')->findOneByAlias('news');
	  
	  $q = Doctrine_Query::create()
			 ->select('c.*')
			 ->from('VTNSCmsCategory c')
			 ->addWhere('c.parent_id = ?', $cms_category_news->cms_category_id);
			 
	  $cms_categories = $q->fetchArray();
	  foreach ($cms_categories as $cms_category)
	  {
		  $c = Doctrine_Query::create()
				 ->select('c.*')
				 ->from('VTNSCms c')
				 ->addWhere('c.cms_category_id = ?', $cms_category['cms_category_id']);
		  $this->cms_arr = $c->fetchArray();
		  if (sizeof($this->cms_arr) == 1)
			  foreach ($this->cms_arr as $cms_temp)
			  	$this->cms = Doctrine_Core::getTable('VTNSCms')->find($cms_temp['cms_id']);
		  break;
	  }
	  $this->cms_categories = $cms_categories;
	  // $this->setTitle("Tin tức");
	  $this->sort_by = $request->getParameter('sort_by', 'created_at');
      if ($request->getParameter('sort_order', 1))
		  $this->sort_order = 'desc';
      else
          $this->sort_order = 'asc';
	  
	  if (Doctrine_Core::getTable('VTNSCms')->hasField($this->sort_by))
	      if (isset($c))
              $c->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
	  	  
	  $this->pager = new sfDoctrinePager('VTNSCms', 5);
	  if (isset($c)) 
	      $this->pager->setQuery($c);
	  $this->pager->setPage($request->getParameter('page', 1));
	  $this->pager->init();
	  if ($request->isXmlHttpRequest()) {
	  	  return 'Ajax';
	  } else return SfView::SUCCESS;
  }
  
  public function executePermalink(sfWebRequest $request) 
  {
	  // DEFAULT
	  $this->navbars = vtnsSlot::getNavBars();
	  $this->rightBlocks = vtnsSlot::getRightBlocks();
	  $this->menuBottoms = vtnsSlot::getMenuBottoms();
	  $this->bottom = vtnsSlot::getBottom();

	  $cms_category_id = $request->getParameter("cms_category_id");
	  if (preg_match("/^[0-9]+/", $cms_category_id, $match))
		  $cms_category_id = $match[0];
	  
	  $cms_id = $request->getParameter("cms_id");
	  if (preg_match("/^[0-9]+/", $cms_id, $match))
		  $cms_id = $match[0];
	  $this->cms_id = $cms_id;
	  
	  $tag_id = $request->getParameter("tag_id");
	  if (preg_match("/^[0-9]+/", $tag_id, $match))
		  $tag_id = $match[0];
	  
	  $this->cms_category_id = $cms_category_id;
	  $q = Doctrine_Query::create()
	  		->from('VTNSCmsCategory c')
			->where('cms_category_id = ?', $cms_category_id);
	  $this->cms_category_default = $q->fetchArray();	  
	  
	  $this->cms_category_default = $this->cms_category_default[0];
	  $page_title = "";
	  $page_key = "";
	  
	  $response = $this->getResponse();
	  if (isset($tag_id)) 
	  {
		  $this->act = "cms_list";
		  $this->tag_object = Doctrine_Core::getTable('VTNSCmsTag')->findOneById($tag_id);
		  $q = Doctrine_Query::create()
 				 ->from('VTNSCms c')
				 ->innerJoin('c.CmsTags g WITH g.tag LIKE "'.$this->tag_object->tag.'"');
				 
		  $this->cms_arr = $q->fetchArray();
		  
	  }
	  else if (isset($cms_id))
	  {
		  $this->act = "cms_view";  
		  $this->cms = Doctrine_Core::getTable('VTNSCms')->find($cms_id);
		  $page_title = $this->cms->name." - ";
		  $page_key = $this->cms->name.", ";
		  
		  $response->addMeta('og:image', 'http://'.$_SERVER['HTTP_HOST'].'/uploads/cms/'.$this->cms->picture);
		  
		  $qc = Doctrine_Query::create()
					->from('VTNSCms c')
					->addWhere('c.created_at < ?', $this->cms->created_at)
					->orderBy('c.created_at desc')
					->limit(7);
		  $this->cms_olders = $qc->fetchArray();
	  }
	  else
	  {
		  $q = Doctrine_Query::create()
 				 ->from('VTNSCms c')
 				 ->addWhere('c.cms_category_id = ?', $cms_category_id);
				 
		  $this->cms_arr = $q->fetchArray();
		  
		  foreach ($this->cms_arr as $cms_temp) {
			$this->cms = Doctrine_Core::getTable('VTNSCms')->find($cms_temp['cms_id']);
			break;
		  }
		  $this->act = "cms_list";
	  }
	  
	  if (isset($this->cms)) {
		$this->cms_tags = $this->cms->getCmsTags();
		$this->cms->views_count ++;
		$this->cms->save();
	  }
	  
	  if (!isset($tag_id)) {
		  $cms_category = Doctrine_Core::getTable('VTNSCmsCategory')->find($cms_category_id);
		  $page_title .= $cms_category->name." - ";
	  }
	  
	  if ($request->hasParameter("send") && $request->getParameter("send") == "Send")
	  {
		  $txt_name = $request->getParameter("txt_name");
		  $txt_email = $request->getParameter("txt_email");
		  $txt_title = $request->getParameter("txt_title");
		  $txt_content = $request->getParameter("txt_content");
		  
		  $body = '
		  Contact details from website <a href="http://'.$_SERVER['HTTP_HOST'].'">http://'.$_SERVER['HTTP_HOST'].'</a> <br />
		  <br />
		  <table>
		  <tr><td><b>Name</b></td><td> : '.$txt_name.' </td></tr>
		  <tr><td><b>Email</b></td><td> : '.$txt_email.'</td></tr>
		  <tr><td><b>Title</b></td> : '.$txt_title.' </td></tr>
		  <tr><td><b>Content</b></td></tr>
		  <tr><td colspan="2">'.$txt_content.'</td></tr>
		  </table><br />
		  ';
		  
		$message = Swift_Message::newInstance()
		  ->setFrom('contact@vtns.vn')
		  ->setTo($txt_email)
		  ->setSubject('Contact from '.$_SERVER['HTTP_HOST'])
		  ->setBody($body)
		  ->setContentType('text/html')
		;
		 
		$this->getMailer()->send($message);
		$this->notify = 'sendSuccess';
	  }
	  
	  $response->setContentType('text/html');
	  $response->setHttpHeader('Content-Language', 'vi');
	  
      $response->addMeta('robots', 'index, follow');
	  if (isset($cms_id) || (!isset($tag_id) && sizeof($this->cms_arr) == 1)) {
		  $response->addMeta('description', $this->cms->description);
	  }
	  else {
		  $response->addMeta('description', $page_title.'Tin tức - sự kiện Công ty TNHH giải pháp mạng du lịch VTNS');
	  }
      $response->addMeta('keywords', $page_key.'VTNS, tin tức, sự kiện, hoạt động, giải pháp, công nghệ, website, thiết kế website, Smart Shop, Business website, phần mềm, tư vấn, data mining, hosting, domain, shop online, prestashop việt nam, joomla việt nam, shop giá rẻ, giới thiệu doanh nghiệp, da nang, thiết kế website đà nẵng, xuất bản phần mềm đà nẵng');
	  
	  // $this->setTitle($page_title."Tin tức - Hoạt động");
	  
	  $this->sort_by = $request->getParameter('sort_by', 'created_at');
      if ($request->getParameter('sort_order', 1))
          $this->sort_order = 'desc';
      else
          $this->sort_order = 'asc';
	  
	  if (Doctrine_Core::getTable('VTNSCms')->hasField($this->sort_by))
	      if (isset($q))
              $q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
	  
	  $this->pager = new sfDoctrinePager('VTNSCms', 5);
	  if (isset($q)) 
	      $this->pager->setQuery($q);
	  $this->pager->setPage($request->getParameter('page', 1));
	  $this->pager->init();
	  if ($request->isXmlHttpRequest()) {
	  	  return 'Ajax';
	  } else return SfView::SUCCESS;
  }
}